<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends MY_Controller {

    public function __construct() {
      parent::__construct();
      // Ako korisnik nije ulogovan nemoze na stranice profila
      if( false == $this->session->userdata('user_logged') )
         redirect(base_url(), 'refresh');
      
      $this->load->model('users_mod');
      $this->load->model('cities_mod');
      $this->load->model('images_mod');
      $this->load->model('friends_mod');
      $this->load->model('favorites_mod');
    }
	/**
	 *
	 */
	public function index()
	{   
        $data['title'] = lang('profile_title');
        $data['keywords'] = lang('profile_keywords');
        $data['description'] = lang('profile_description');
        $id = $this->session->userdata('user_id');
        $data['user'] = $this->users_mod->getUserData($id,false);
        $data['images'] = $this->images_mod->getUserImages($id,false);
        
        $data['main_image'] = $this->images_mod->getMainImage($id);
        
        $data['menu_active'] = "profile";
        $data['contents']  = "profile/home";   
		$this->load->view('template/main',$data);
	}   
   
    /**
     * 
     * @param int $id - ID usera (folder name)
     * @param string $name - image name
     */
    public function image($dir=NULL,$id=NULL,$name=NULL)
    {
        if( $dir!="" )
          $img_url = base_url().'images/'.$id.'/'.$dir."/".$name;
        else
          $img_url = base_url().'images/'.$id."/".$name;
        
        $img_path = $this->config->item('image_path').$id."/".$name;
        $img = @fopen($img_url, 'r');
        $info = @getimagesize($img_path);
        $type = $info['mime'];
        //print_r($info);
        
        if ($img)
        {
            header('Content-Type: '.$type);
            fpassthru($img);
        }  
        
    }
    
   
    
    public function view_profile($username=NULL)
    {
        if( NULL == $username )// nije prosledjeno username => idi na HOME page
          redirect(base_url());
        
        $user = $this->users_mod->getUserDataByUsername($username,false);
        
        if( NULL == $user ) // Nema podataka za tog usera idi na HOME page
          redirect(base_url());
        
        $data['user'] = $user;
        $data['images'] = $this->images_mod->getUserImages($user->id,false);
        $data['main_image'] = $this->images_mod->getMainImage($user->id);
        $id = $this->session->userdata('user_id'); // My ID
        $data['is_friend'] = $this->friends_mod->isMyFriend($user->id);
        $data['is_favorite'] = $this->favorites_mod->isMyFavorite($user->id);
        
        $this->users_mod->setViewProfile( $id, $user->id ); // Postavi da sam gledao taj profil
        //$this->users_mod->getNumViewMyProfile();
        $data['title'] = lang('profile_title');
        $data['keywords'] = lang('profile_keywords');
        $data['description'] = lang('profile_description');
        
        $data['menu_active'] = "";// "profile";
        $data['contents']  = "profile/view_profile";   
		$this->load->view('template/main',$data);
    }
    
    public function promo_code()
    {       
      
        $data['title'] = lang('profile_title');
        $data['keywords'] = lang('profile_keywords');
        $data['description'] = lang('profile_description');
        $id = $this->session->userdata('user_id');
        $data['user'] = $this->users_mod->getUserData($id,false);
        $data['images'] = $this->images_mod->getUserImages($id,false);
        
        $data['main_image'] = $this->images_mod->getMainImage($id);
        
        $data['menu_active'] = "profile";
        $data['contents']  = "profile/promo_code";   
		$this->load->view('template/main',$data);
    }
    
    public function accept_promo_code()
    {
            
      try{
          $code = $this->input->post('code');
          $id = $this->session->userdata('user_id');
          if( true == $this->users_mod->acceptPromoCode($id,$code) )
          {
            $response = array(
                'ind' => 1,
                'msg' => lang('code_accepted')
            );
          }
          else
          {
            $response = array(
                'ind' => 0,
                'msg' => lang('code_not_accepted')
            );
          }
        } 
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
        
    }
    
    public function whos_viewed_my_profile()
    {
        $data['title'] = lang('profile_title');
        $data['keywords'] = lang('profile_keywords');
        $data['description'] = lang('profile_description');
               
        
        $id = $this->session->userdata('user_id');
        $data['user'] = $this->users_mod->getUserData($id,false);
        $data['main_image'] = $this->images_mod->getMainImage($id);
        if( /* $this->session->userdata('is_pay_who_viewed_profile') || */ $data['user']->coins >= $this->config->item('who_viewed_profile_cost') )
        {
          $data['viewed_users'] = $this->users_mod->getWhosViewedMyProfile();          
        }
        else {
          $data['viewed_users'] = NULL;
          $data['no_money'] = true;
        }
        
        $data['menu_active'] = "profile";
        $data['contents']  = "profile/whos_viewed";   
		$this->load->view('template/main',$data);
    }
    
    public function view_profile_photo($username=NULL)
    {
        if( NULL == $username )
          redirect(base_url());
        
        $user = $this->users_mod->getUserDataByUsername($username,false);
               
        if( NULL == $user ) // Nea podataka za tog usera idi na HOME page
          redirect(base_url());
        
        $data['user'] = $user;
        $data['images'] = $this->images_mod->getUserImages($user->id,false);
        $data['main_image'] = $this->images_mod->getMainImage($user->id);
        $id = $this->session->userdata('user_id'); // My ID
        $data['is_friend'] = $this->friends_mod->isMyFriend($user->id);
        $data['is_favorite'] = $this->favorites_mod->isMyFavorite($user->id);
        
        $data['title'] = lang('profile_title');
        $data['keywords'] = lang('profile_keywords');
        $data['description'] = lang('profile_description');
        
        $data['menu_active'] = "";//"profile";
        $data['contents']  = "profile/view_profile_photo";   
		$this->load->view('template/main',$data);
    }
    
    
    
    /**
     * F-ja za izmenu profila
     * @param int $user_id - user id koji moze admin da prosledi kako bi menjao bilo koji profil
     */
    public function edit_profile($user_id = NULL)
    {
        // Ako je user admin onda moze da menja sve profile. Prosledjuje se id profila kroz get
        // profile/edit_profile/xx  gde je xx id usera koji admin zeli da edituje
        if( true == $this->session->userdata('admin_logged') && $user_id !=NULL ) 
        {
          $id = $user_id;
          $data['admin_edit'] = true;
        }
        else // Nije admin. obican user
        {
          $id = $this->session->userdata('user_id');
          $data['admin_edit'] = false;
        }
        $data['response'] = "";
        
        // Process submit form. Save data
        if( $this->input->post('edit_save')!=NULL  )
        {
          $user = $this->input->post('user');
          if( true == $this->users_mod->saveUser($user) ) // Ako se uspesno sacuvaju podaci
            redirect( base_url().'profile.htm' ); // Idi na stranu profila
          else
            $data['response'] = lang('data_not_save'); // Ako nije prikazi gresku
        }
        
        $user = $this->users_mod->getUserData($id);
        $birthdate = $user['birthdate'];
        
        $user['birthdate'] = array();
        $user['birthdate']['year'] = substr($birthdate, 0,4);
        $user['birthdate']['month'] = substr($birthdate, 5,2);
        $user['birthdate']['day'] = substr($birthdate, 8,2);
        
        $data['user'] = $user;
        //$data['states'] = $this->users_mod->getStatesList();
        $data['cities'] = $this->cities_mod->getCitiesList();
        $data['main_image'] = $this->images_mod->getMainImage($id);
        
        $data['title'] = lang('profile_title');
        $data['keywords'] = lang('profile_keywords');
        $data['description'] = lang('profile_description');
        $data['menu_active'] = "profile";
        $data['contents']  = "profile/edit";   
		$this->load->view('template/main',$data);
    }
    
    
    
    public function edit_photo($user_id = NULL)
    {
        // Ako je user admin onda moze da menja sve profile. Prosledjuje se id profila kroz get
        // profile/edit_profile/xx  gde je xx id usera koji admin zeli da edituje        
        if( true == $this->session->userdata('admin_logged') && $user_id !=NULL ) 
        {
          $id = $user_id;
          $data['admin_edit'] = true;
        }
        else // Nije admin. obican user
        {
          $id = $this->session->userdata('user_id');
          $data['admin_edit'] = false;
        }
        
        $data['title'] = lang('profile_title');
        $data['keywords'] = lang('profile_keywords');
        $data['description'] = lang('profile_description');
        //$data['response'] = "";
        $data['user'] = $this->users_mod->getUserData($id);
        $data['images'] = $this->images_mod->getUserImages($id,false);
        $data['main_image'] = $this->images_mod->getMainImage($id);
        
        $data['menu_active'] = "profile";        
        $data['contents']  = "profile/edit_gallery";   
		$this->load->view('template/main',$data);
    }
    
    /**
     *  AJAX f-ja za izmenu samo jedne stavka profila.
     */
    public function edit_item()
    {
        try{
          
          $field = $this->input->post('field');
          $data = $this->input->post('data');
          
          if( true == $this->users_mod->editItem($field,$data) )
          {
            $response = array(
                'ind' => 1
            );
          }
          else
          {
            $response = array(
                'ind' => 0,
                'msg' => lang('data_not_save')
            );
          }
        } 
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    public function addToSpotlight()
    {
        try{
                   
          $data = $this->input->post('data');
          $ret = $this->users_mod->addToSpotlight();
          if( 1 == $ret )
          {            
            $html = $this->load->view('modules/elastislide_carousel',NULL,true);
            $response = array(
                'ind' => 1,
                'msg' => lang('you_add_in_spotlight'),
                'html' => $html
            );
          }
          else if( -2 == $ret )
          {
            $response = array(
                'ind' => -2,
                'msg' => sprintf(lang('user_no_coins'),base_url()."payment.htm")
            );
          }
          else if( -3 == $ret )
          {
             $response = array(
                'ind' => -3,
                'msg' => lang('user_alredy_in_spotlight')
            );
          }
          else
          {
            $response = array(
                'ind' => 0,
                'msg' => lang('data_not_save')
            );
          }
        } 
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
       
} // end class
/* End of file profile.php */
/* Location: ./application/controllers/profile.php */